#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简单的Markdown到HTML转换器
"""

import os
import re
import sys
import markdown
from markdown.extensions import toc

def main():
    # 输入文件路径
    input_file = "租赁合同管理系统操作手册_新_完善版.md"
    
    # 检查文件是否存在
    if not os.path.exists(input_file):
        print(f"错误：文件 '{input_file}' 不存在")
        return
    
    # 读取Markdown内容
    try:
        with open(input_file, 'r', encoding='utf-8') as f:
            md_content = f.read()
        print("成功读取Markdown文件")
    except Exception as e:
        print(f"读取文件失败: {e}")
        return
    
    # 提取标题
    title_match = re.search(r'^#\s+(.+)$', md_content, re.MULTILINE)
    title = title_match.group(1) if title_match else "租赁合同管理系统操作手册"
    print(f"文档标题: {title}")
    
    # 转换Markdown为HTML
    try:
        # 配置markdown扩展
        md = markdown.Markdown(extensions=['extra', 'toc'])
        html_content = md.convert(md_content)
        toc_html = md.toc
        print("成功转换Markdown为HTML")
    except Exception as e:
        print(f"转换失败: {e}")
        return
    
    # 生成完整HTML
    full_html = f'''
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{title}</title>
    <style>
        body {{
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            line-height: 1.6;
            color: #333;
            max-width: 1000px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f8f9fa;
        }}
        .paper {{
            background: white;
            padding: 40px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }}
        h1, h2, h3, h4, h5, h6 {{
            color: #2c3e50;
            margin-top: 1.5em;
            margin-bottom: 0.5em;
        }}
        h1 {{ font-size: 2em; border-bottom: 2px solid #3498db; padding-bottom: 0.3em; }}
        h2 {{ font-size: 1.5em; border-bottom: 1px solid #eee; }}
        a {{ color: #3498db; text-decoration: none; }}
        a:hover {{ text-decoration: underline; }}
        ul, ol {{ margin-left: 2em; }}
        pre {{ background: #f5f5f5; padding: 15px; border-radius: 5px; overflow-x: auto; }}
        code {{ font-family: Consolas, Monaco, monospace; }}
        table {{ border-collapse: collapse; width: 100%; margin: 20px 0; }}
        th, td {{ border: 1px solid #ddd; padding: 8px 12px; text-align: left; }}
        th {{ background-color: #f2f2f2; }}
        blockquote {{ border-left: 4px solid #3498db; padding-left: 1em; margin: 1em 0; color: #666; }}
        img {{ max-width: 100%; height: auto; }}
        .toc {{ background: #f8f9fa; padding: 20px; border-radius: 5px; margin-bottom: 30px; }}
        .toc h2 {{ margin-top: 0; }}
        .toc ul {{ list-style-type: none; }}
        .toc ul ul {{ margin-left: 1.5em; }}
        @media (max-width: 768px) {{
            body {{ padding: 10px; }}
            .paper {{ padding: 20px; }}
        }}
    </style>
</head>
<body>
    <div class="paper">
        {toc_html}
        <main>
            {html_content}
        </main>
    </div>
    <script>
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {{
            anchor.addEventListener('click', function (e) {{
                e.preventDefault();
                const target = document.querySelector(this.getAttribute('href'));
                if (target) {{
                    target.scrollIntoView({{ behavior: 'smooth' }});
                }}
            }});
        }});
    </script>
</body>
</html>
'''
    
    # 写入HTML文件
    output_file = "租赁合同管理系统操作手册_新_完善版.html"
    try:
        with open(output_file, 'w', encoding='utf-8') as f:
            f.write(full_html)
        print(f"转换成功！HTML文件已保存至: {output_file}")
    except Exception as e:
        print(f"写入文件失败: {e}")
        return

if __name__ == '__main__':
    main()
